package pri.zxx.webdemo.controllers;

import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import pri.zxx.webdemo.aop.LimitIp;
import pri.zxx.webdemo.config.properties.SwaggerProperties;
import pri.zxx.webdemo.entity.SysUser;
import pri.zxx.webdemo.entity.Type;
import pri.zxx.webdemo.entity.vo.PageDto;
import pri.zxx.webdemo.entity.vo.ResultData;
import pri.zxx.webdemo.mappers.TypeMapper;
import pri.zxx.webdemo.services.SysUserService;
import pri.zxx.webdemo.utils.GsonUtil;

import jakarta.servlet.http.HttpServletRequest;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

/**
 * @author Twilight
 * @desc
 * @createTime 2020-05-23-21:14
 */
@Controller
@Slf4j
@RequestMapping(value = "/sysuser")
@Tag(name =  "LEARN_DEMO-增删改查")
public class SysUserController {

	@Autowired
	private SysUserService sysUserService;

	@Autowired
	private SwaggerProperties swaggerProperties;

	@Resource
	private TypeMapper typeMapper;

	@GetMapping(value = "/listByCondition")
	public ModelAndView listByCondition(Integer page, Integer size, HttpServletRequest request) {

		Object userId = request.getAttribute("userId");

		ModelAndView mav = new ModelAndView();
		mav.setViewName("sysUser");
		PageDto pageDto = PageDto.get(page, size);

		Integer total = sysUserService.listByConditionCount();
		pageDto.setTotalNum(total);

		List<SysUser> sysUsers = sysUserService.listByCondition(pageDto.pageIndex, pageDto.size);

		mav.addObject("sysUsers", sysUsers);
		mav.addObject("pageDto", pageDto);
		return mav;
	}

	@GetMapping(value = "/listjson")
	@ResponseBody
	@LimitIp(allow = "0:0:0:0:0:0:0:1")
	public ResultData<SysUser> listByConditionJson(Integer page, Integer size) {
		PageDto pageDto = PageDto.get(page, size);

		Integer total = sysUserService.listByConditionCount();
		pageDto.setTotalNum(total);

		List<SysUser> sysUsers = sysUserService.listByCondition(pageDto.pageIndex, pageDto.size);
		return ResultData.success(sysUsers).page(pageDto);
	}

	@GetMapping(value = "insertType")
	@ResponseBody
	public ResultData<Integer> insert(Type type) {
		int insert = typeMapper.insertSelective(type);
		return ResultData.success(insert);
	}

	@GetMapping(value = "typeList")
	@ResponseBody
	public ResultData<List<Type>> list(Integer page, Integer size) {
		PageDto pageDto = new PageDto(page, size);
		List<Type> insert = typeMapper.listByPage(pageDto.getOffset(), pageDto.getSize());
		int total = typeMapper.listByPageCount();
		pageDto.setTotalNum(total);
		return ResultData.success(insert).page(pageDto);
	}

	@GetMapping(value = "test")
	@ResponseBody
	public ResultData<String> test() {
		System.out.println(GsonUtil.toJson(swaggerProperties));
		return ResultData.success();
	}

	@RequestMapping(value = "back")
	@ResponseBody
	public ResultData<String> back(HttpServletRequest request) {
		log.warn("---->>收到微信异步回调");
		String resXml;//接受数据
		try {
			InputStream inputStream = request.getInputStream();
			//将InputStream转换成xmlString
			BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
			StringBuilder sb = new StringBuilder();
			String line;
			while ((line = reader.readLine()) != null) {
				sb.append(line).append("\n");
			}
			resXml = sb.toString();
			inputStream.close();
			log.warn("微信回调数据：{}", resXml);
		} catch (Exception e) {
			log.error("微信手机支付失败:", e);
			return ResultData.success();
		}
		return ResultData.success();
	}
}
